/**
 *
 */
package com.siteseat.common.exception;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

/**
 */
public class SSException extends RuntimeException {

  /**
   *
   */
  private static final long serialVersionUID = -954729593211188692L;

  /**
   * 
   */
  public SSException() {
  }

  /**
   * @param message
   */
  public SSException( String message ) {
    super( message );
  }

  /**
   * @param cause
   */
  public SSException( Throwable cause ) {
    super( cause );
  }

  /**
   * @param message
   * @param cause
   */
  public SSException( String message, Throwable cause ) {
    super( message, cause );
  }

  public static Throwable analyseCause( Throwable e ) {
    Throwable cause = e;

    while( ( cause != null ) && ( cause.getCause() != cause ) ) {
      if( cause instanceof UndeclaredThrowableException ) {
        cause = ( (UndeclaredThrowableException)cause ).getUndeclaredThrowable();
      }

      if( cause instanceof InvocationTargetException ) {
        cause = ( (InvocationTargetException)cause ).getTargetException();
      }

      if( cause instanceof ExternalException ) {
        break;
      }

      if( cause.getCause() == null ) {
        break;
      }

      cause = cause.getCause();
    }

    return cause;
  }

}
